fix: prevent AbortController GC when redirect is 'error'#4750
Open
fix: prevent AbortController GC when redirect is 'error'#4750
Conversation
When fetch() is called with redirect: 'error', the requestObject could be garbage collected while the fetch was still in progress. This caused the internal AbortController to be collected, which triggered a FinalizationRegistry callback that removed the abort listener from the user's signal, making abort signals ineffective. The fix stores requestObject in fetchParams to keep it alive for the entire duration of the fetch operation. Fixes: #4627 Signed-off-by: Matteo Collina <hello@matteocollina.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4750 +/- ##
==========================================
+ Coverage 93.21% 93.26% +0.04%
==========================================
Files 109 109
Lines 33982 34104 +122
==========================================
+ Hits 31678 31807 +129
+ Misses 2304 2297 -7 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
metcoder95
approved these changes
Jan 22, 2026
Uzlopak
reviewed
Jan 22, 2026
Co-authored-by: Aras Abbasi <aras.abbasi@googlemail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #4627
When
fetch()is called withredirect: 'error', therequestObject(theRequestinstance) could be garbage collected while the fetch was still in progress. TherequestObjectholds an internalAbortControllervia thekAbortControllerproperty. WhenrequestObjectwas GC'd, theAbortControllerwas also collected, triggering aFinalizationRegistrycallback that removed the abort listener from the user's signal. This caused abort signals to have no effect, resulting in connection leaks for SSE streams.Changes
The fix stores a reference to
requestObjectinfetchParams, which stays alive for the entire duration of the fetch operation. This prevents premature garbage collection of therequestObjectand its internalAbortController.requestObjectto thefetching()functionrequestObjectparameter tofetching()function signaturerequestObjectinfetchParamsobjectTest Plan
test/fetch/issue-4627.jsthat requires--expose-gcflagredirect: 'error', triggers GC, aborts, and verifies no data is received after abortabort should work with redirect: error✅abort should work without redirect: error (control test)✅